﻿using AntdUI;
using ImportPlcPointPosition.Controls;
using ImportPlcPointPosition.Forms;
using ImportPlcPointPosition.Utils;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;

namespace ImportPlcPointPosition
{
    public partial class MainLayout : BorderlessForm
    {
        public MainLayout()
        {
            InitializeComponent();
            this.Text = windowBar1.Text;

            AntdUI.Config.ShowInWindow = true;
          
            App.MainLayout = this;
            this.Width = (int)(Screen.PrimaryScreen.WorkingArea.Width/ AntdUI.Config.Dpi - 150);

            this.Load += MainLayout_Load;


           
        }

        Dictionary<string, object> dicControls = new Dictionary<string, object>();
     
        private void MainLayout_Load(object sender, EventArgs e)
        {
            AntdUI.Config.IsLight = true;
            this.BackColor = Color.White;
            this.ForeColor = Color.Black;
            AntdUI.Config.ScrollBarHide = true;

            this.leftMenu.SelectIndex(-1);
            this.leftMenu.SelectChanged += LeftMenu_SelectChanged;
            this.leftMenu.SelectIndex(0);
            this.StartPosition = FormStartPosition.CenterScreen;

            this.FormClosing += MainLayout_FormClosing;


            this.Icon = ImageToIcon(windowBar1.Icon);
            

        }
        public Icon ImageToIcon(Image image)
        {
            // 创建一个与原始图像大小相同的空白图像
            Bitmap iconBitmap = new Bitmap(64, 64);
            // 使用原始图像绘制空白图像
            using (Graphics graphics = Graphics.FromImage(iconBitmap))
            {
                graphics.CompositingMode = System.Drawing.Drawing2D.CompositingMode.SourceCopy;
                graphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
                graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                graphics.DrawImage(image, 0, 0, 64, 64);
            }
            using (Icon icon = Icon.FromHandle(iconBitmap.GetHicon()))
            {
                // 保存图标到文件
                //using (FileStream fs = new FileStream("icon.ico", FileMode.Create, FileAccess.Write))
                //{
                //    icon.Save(fs);
                //}
                // 复制Icon，因为GetHicon()在调用后会释放图标资源
                return Icon.FromHandle(icon.Handle);
            }

        }


        private void MainLayout_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (dicControls.ContainsKey("pointConfig"))
            {
                var dpp = dicControls["pointConfig"] as DataPositionPage;
                dpp.ClosePlc();

            }
        }

        private object control;
        private void LeftMenu_SelectChanged(object sender, MenuSelectEventArgs e)
        {
            var name = e.Value.Tag;
            control = new BaseUserControl();
            pnl_Content.Controls.Clear();
            if (!dicControls.ContainsKey(name.ToString()))
            {
                switch (name)
                {
                    case "plcConfig":
                        control = new PlcManagePage();
                        break;
                    case "pointConfig":
                        control = new DataPositionPage();
                        break;
                    case "blockDbConfig":
                        control = new PlcBlockPage();
                        break;
                    case "collectDataConfig":
                        control = new CollectDataConfigPage();
                        break;
                    case "techniqueTemplate":
                        control = new TechniqueTemplatePage();
                        break;
                    case "techniqueRoad":
                        control = new TechnicalFlowPage();
                        break;
                    default:
                        break;
                }
                dicControls[name.ToString()] = control;
            }
          

            if (dicControls.ContainsKey(name.ToString()))
            {
                var ctl = dicControls[name.ToString()] as Control;
                //容器添加控件，需要调整dpi
                ctl.Dock = DockStyle.Fill;
               // AutoDpi(control);
                pnl_Content.Controls.Add(ctl);
                if(ctl is BaseUserControl)
                    (ctl as BaseUserControl).ReflushPage();



            }
        }
    }
}
